

using System;
using System.Web.Security;
using System.Collections.Generic;

using Grr;
using Grr.Models;

using Castle.MonoRail.Framework;


namespace Grr.Controllers {

	[Layout("default_w_sidebar"), Rescue("generalerror")]
	public class GrurrahController : SmartDispatcherController {
		public void Home()
		{
			Grr.Models.Feature feature = Grr.Models.Feature.GetCurrentFeature();

			PropertyBag["CurrentFeature"] = feature;
		}

		public void Explore()
		{
			List<Tag> tags;
			SearchResult popular;
			SearchResult recent;
			
			Grr.Models.Explore.GetPopularAndRecentAndTags(5, out popular, 5, out recent, out tags);

			PropertyBag["Popular"] = popular;
			PropertyBag["Recent"] = recent;
			PropertyBag["TagsList"] = tags;
		}

		public void Create()
		{
		}

		public void Search(TaintedString query)
		{
			if (query != null && !query.IsEmpty)
			{
				string q = query != null ? query.Clean() : String.Empty;
				SearchResult result = Grr.Models.Search.Query(q, 10, 0);
				PropertyBag["SearchTerm"] = q;
				PropertyBag["SearchResult"] = result;
			}

		}

		public void Grurrah(TaintedString friendly_name)
		{
			Grr.Models.Grurrah grurrah = null;
			if (friendly_name != null && !friendly_name.IsEmpty) {
				string name = StringUtil.ToDb(friendly_name.Clean());
				grurrah = Grr.Models.Grurrah.QueryForByName(name, Membership.GetUser ());
				PropertyBag["grurrah"] = grurrah;
			}

			if (grurrah == null)
				RenderView("vacant");
		}

		public void Tags(TaintedString tag)
		{
			if (tag != null && !tag.IsEmpty) {
				string t = tag.CleanNoPunc();
				SearchResult result = Tag.GetGrurrahsForTag(t, 10, 0);
				PropertyBag["Tag"] = t;
				PropertyBag["TagResult"] = result;

				RenderView("tag");
			}
		}

		public void People(TaintedString person)
		{
			if (person != null && !person.IsEmpty) {
				string p = StringUtil.ToDb (person.Clean());
				PropertyBag["Person"] = p;

				// We need to go through the membership API instead of joining
				// the user table, because the read connection does not have 
				// permission to look at the users table

				MembershipUser user = Membership.GetUser(p);
				if (user == null) {
					RenderView("no_such_person");
					return;
				}

				SearchResult created;
				SearchResult doing;
				SearchResult want_to_do;

				Grr.Models.People.GetResultsForPerson((long)user.ProviderUserKey, 10, out created, out doing, out want_to_do);

				PropertyBag["Created"] = created;
				PropertyBag["Doing"] = doing;
				PropertyBag["WantToDo"] = want_to_do;

				RenderView("person");
			}
		}

		public void Feature(TaintedString friendly_name)
		{
			Feature feature = null;
			if (friendly_name != null && !friendly_name.IsEmpty) {
				string name = StringUtil.ToDb(friendly_name.Clean());
				feature = Grr.Models.Feature.QueryForByName(name);
			}

			if (feature == null)
				feature = Grr.Models.Feature.GetCurrentFeature();

			PropertyBag["feature"] = feature;
		}

		public void ChangePassword()
		{
		}

		public void ForgotPassword()
		{
		}
	}
}

